package com.work.saturn.assets;


import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.immo.libcommon.base.BaseBean;
import com.immo.libcommon.base.BaseHeadActivity;
import com.immo.libcommon.constant.BaseUrl;
import com.immo.libcommon.http.NetworkRequest;
import com.work.saturn.R;
import com.immo.libcommon.constant.RouterUtil;
import com.work.saturn.bean.GASBean;
import com.work.saturn.mine.ChangeGasActivity;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionNo;
import com.yanzhenjie.permission.PermissionYes;
import com.yzq.zxinglibrary.android.CaptureActivity;
import com.yzq.zxinglibrary.bean.ZxingConfig;
import com.yzq.zxinglibrary.common.Constant;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.widget.EditText;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.immo.libcommon.utils.InputValidationUtils.inputIsEmpty;

/**
 * @author Administrator
 * @content 转出
 * @date 2018/7/11
 */
@Route(path = RouterUtil.ASSETS_TURN_OUT)
public class AssetsTurnOutActivity extends BaseHeadActivity {
    @BindView(R.id.turn_out_account)
    EditText account;
    @BindView(R.id.turn_out_num)
    EditText mTurnOutNum;
    @BindView(R.id.turn_out_dw)
    TextView mTurnOutDw;
    @BindView(R.id.turn_out_bz)
    EditText mTurnOutBz;
    private String name;
    private static final int REQUEST_CODE_PERMISSION_MULTI = 105;
    private static final int REQUEST_CODE_SCAN = 100;
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_turn_out);
        ButterKnife.bind(this);
        name = getIntent().getStringExtra("name");
        userId=getIntent().getStringExtra("userId");
        setTitle(name + "转账");
        setTitleColor(R.color.colorYellow);
        setTitleLeft(R.mipmap.back_finish);
        setTitleRight(null, R.mipmap.acode);
        mTurnOutDw.setText(name);
    }

    @Override
    protected void onRightClick() {
        super.onRightClick();
        getPerMission();
    }

    /**
     * 获取权限
     */
    public void getPerMission() {
        AndPermission.with(this)
                .requestCode(REQUEST_CODE_PERMISSION_MULTI).permission(
                Manifest.permission.CAMERA
        ).callback(this).rationale((requestCode, rationale) -> AndPermission.rationaleDialog(AssetsTurnOutActivity.this, rationale).show()).start();
    }

    @SuppressWarnings("unused")
    @PermissionYes(REQUEST_CODE_PERMISSION_MULTI)
    private void getMultiYes(@NonNull List<String> grantedPermissions) {
        Intent intent = new Intent(AssetsTurnOutActivity.this, CaptureActivity.class);
        ZxingConfig zxingConfig = new ZxingConfig();
        intent.putExtra("zxingConfig", zxingConfig);
        startActivityForResult(intent, REQUEST_CODE_SCAN);
    }

    @SuppressWarnings("unused")
    @PermissionNo(REQUEST_CODE_PERMISSION_MULTI)
    private void getMultiNo(@NonNull List<String> deniedPermissions) {
        toast("此功能需要获取相机权限");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_SCAN && resultCode == RESULT_OK) {
            if (data != null) {
                String content = data.getStringExtra(Constant.CODED_CONTENT);
                account.setText(content);
            }
        }

    }

    /**
     * 输入的本地判断
     *
     * @return true 通过 false 不通过
     */
    private boolean isCheck() {
        return inputIsEmpty(this, account.getText().toString(), "转出账号不能为空")
                && inputIsEmpty(this, mTurnOutNum.getText().toString(), "转出金额不能为空");
    }


    @OnClick(R.id.turn_in_copy)
    public void onViewClicked() {
        if (isCheck()) {
            HashMap<String, String> params = new HashMap<>();
            params.put("amount", mTurnOutNum.getText().toString());
            params.put("symbol",name);
            new NetworkRequest((Object object, String String) -> {
                if (object instanceof GASBean) {
                    GASBean baseBean = (GASBean) object;
                    if (baseBean.isSuccess()) {
                        PayDetailFragment payDetailFragment = new PayDetailFragment(name,account.getText().toString(),mTurnOutNum.getText().toString(),userId,""+baseBean.getPayload().getPayload().getGas(),1,mTurnOutBz.getText().toString());
                        payDetailFragment.show(getSupportFragmentManager(), "payDetailFragment");
                    }

                }
            }).requestPost(BaseUrl.GET_GAS, AssetsTurnOutActivity.this, JSON.toJSONString(params), GASBean.class, true, false);

//            TurnOutNet();
        }
    }
}
